<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>项目完成情况分析 - 悬浮显示总量</title>
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #1a2a6c, #b21f1f, #1a2a6c);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        .container {
            width: 95%;
            max-width: 1200px;
            background-color: rgba(255, 255, 255, 0.95);
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            padding: 30px;
            animation: fadeIn 0.8s ease-out;
        }
        header {
            text-align: center;
            margin-bottom: 30px;
        }
        h1 {
            font-size: 2.5rem;
            color: #2c3e50;
            margin-bottom: 10px;
            background: linear-gradient(90deg, #3498db, #2c3e50);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }
        .subtitle {
            font-size: 1.1rem;
            color: #7f8c8d;
            max-width: 600px;
            margin: 0 auto;
            line-height: 1.6;
        }
        .chart-container {
            height: 550px;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            background: white;
        }
        .legend {
            display: flex;
            justify-content: center;
            gap: 30px;
            margin-top: 20px;
            font-size: 1.1rem;
        }
        .legend-item {
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .legend-color {
            width: 25px;
            height: 15px;
            border-radius: 3px;
        }
        .controls {
            display: flex;
            justify-content: center;
            gap: 20px;
            margin-top: 25px;
        }
        button {
            padding: 12px 25px;
            font-size: 1.1rem;
            background: linear-gradient(to right, #3498db, #2c3e50);
            color: white;
            border: none;
            border-radius: 50px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
        }
        button:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 15px rgba(0, 0, 0, 0.3);
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        @media (max-width: 768px) {
            .container {
                padding: 15px;
            }
            h1 {
                font-size: 2rem;
            }
            .chart-container {
                height: 450px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>项目完成情况分析</h1>
            <p class="subtitle">悬浮在柱子上查看计划总量、已完成量和完成率。深色部分代表已完成量，浅色部分代表剩余量。</p>
        </header>
        
        <div class="chart-container" id="main"></div>
        
        <div class="legend">
            <div class="legend-item">
                <div class="legend-color" style="background-color: #1e88e5;"></div>
                <span>已完成量</span>
            </div>
            <div class="legend-item">
                <div class="legend-color" style="background-color: #64b5f6;"></div>
                <span>剩余量</span>
            </div>
        </div>
        
        <div class="controls">
            <button id="updateData">更新数据</button>
            <button id="exportChart">导出图表</button>
        </div>
    </div>

    <script>
        // 初始化图表
        const chartDom = document.getElementById('main');
        const myChart = echarts.init(chartDom);
        
        // 示例数据
        const projects = ['项目A', '项目B', '项目C', '项目D', '项目E', '项目F'];
        const planned = [120, 150, 90, 180, 140, 110]; // 计划总量
        const completed = [80, 120, 50, 90, 100, 65];  // 已完成量
        
        // 计算剩余量和完成率
        const remaining = planned.map((total, index) => total - completed[index]);
        const completionRate = planned.map((total, index) => 
            total > 0 ? ((completed[index] / total) * 100).toFixed(1) + '%' : 'N/A'
        );
        
        // 配置项
        const option = {
            tooltip: {
                trigger: 'axis',
                axisPointer: { 
                    type: 'shadow',
                    shadowStyle: {
                        color: 'rgba(150, 150, 150, 0.1)'
                    }
                },
                formatter: function(params) {
                    // 无论悬浮在柱子哪部分，都显示整个柱子信息
                    const project = params[0].name;
                    const total = planned[params[0].dataIndex];
                    const completedVal = completed[params[0].dataIndex];
                    const rate = (completedVal / total * 100).toFixed(1);
                    
                    // 创建HTML格式的提示框
                    return `
                        <div style="text-align: left; font-size: 14px;">
                            <div style="font-weight: bold; font-size: 16px; margin-bottom: 8px; color: #2c3e50;">${project}</div>
                            <div style="display: flex; justify-content: space-between; margin: 5px 0;">
                                <span>计划总量：</span>
                                <span style="font-weight: bold;">${total}</span>
                            </div>
                            <div style="display: flex; justify-content: space-between; margin: 5px 0;">
                                <span>已完成：</span>
                                <span style="font-weight: bold; color: #1e88e5;">${completedVal}</span>
                            </div>
                            <div style="display: flex; justify-content: space-between; margin: 5px 0;">
                                <span>剩余量：</span>
                                <span style="color: #64b5f6;">${total - completedVal}</span>
                            </div>
                            <div style="display: flex; justify-content: space-between; margin: 5px 0;">
                                <span>完成率：</span>
                                <span style="font-weight: bold; color: #27ae60;">${rate}%</span>
                            </div>
                        </div>
                    `;
                }
            },
            legend: { show: false },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                top: '10%',
                containLabel: true
            },
            xAxis: {
                type: 'category',
                data: projects,
                axisLabel: {
                    fontSize: 14,
                    fontWeight: 'bold',
                    color: '#2c3e50'
                },
                axisLine: {
                    lineStyle: { color: '#7f8c8d' }
                }
            },
            yAxis: {
                type: 'value',
                name: '工作量',
                nameTextStyle: {
                    fontSize: 14,
                    padding: [0, 0, 0, 30]
                },
                axisLabel: {
                    fontSize: 13
                },
                splitLine: {
                    lineStyle: { type: 'dashed' }
                }
            },
            series: [
                {
                    name: '已完成量',
                    type: 'bar',
                    stack: 'total',
                    data: completed,
                    itemStyle: {
                        color: '#1e88e5',
                        borderRadius: [5, 5, 0, 0]
                    },
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    },
                    label: {
                        show: true,
                        position: 'insideTop',
                        formatter: '{c}',
                        color: 'white',
                        fontWeight: 'bold',
                        fontSize: 12
                    }
                },
                {
                    name: '剩余量',
                    type: 'bar',
                    stack: 'total',
                    data: remaining,
                    itemStyle: {
                        color: '#64b5f6',
                        borderRadius: [0, 0, 5, 5]
                    },
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowColor: 'rgba(0, 0, 0, 0.3)'
                        }
                    },
                    label: {
                        show: true,
                        position: 'insideBottom',
                        formatter: function(params) {
                            return '总量: ' + (params.value + completed[params.dataIndex]);
                        },
                        color: '#2c3e50',
                        fontWeight: 'bold',
                        fontSize: 12
                    }
                },
                {
                    name: '完成率',
                    type: 'scatter',
                    symbolSize: 0,
                    label: {
                        show: true,
                        position: 'top',
                        formatter: completionRate,
                        color: '#27ae60',
                        fontWeight: 'bold',
                        fontSize: 14,
                        offset: [0, 15]
                    },
                    data: completed
                }
            ]
        };
        
        // 应用配置
        myChart.setOption(option);
        
        // 更新数据按钮事件
        document.getElementById('updateData').addEventListener('click', function() {
            // 生成随机数据
            const newPlanned = planned.map(val => val + Math.floor(Math.random() * 30));
            const newCompleted = newPlanned.map(total => Math.floor(total * (0.3 + Math.random() * 0.6)));
            const newRemaining = newPlanned.map((total, idx) => total - newCompleted[idx]);
            const newRates = newPlanned.map((total, idx) => 
                total > 0 ? ((newCompleted[idx] / total) * 100).toFixed(1) + '%' : 'N/A'
            );
            
            // 更新图表
            myChart.setOption({
                series: [
                    { data: newCompleted },
                    { 
                        data: newRemaining,
                        label: {
                            formatter: function(params) {
                                return '总量: ' + (params.value + newCompleted[params.dataIndex]);
                            }
                        } 
                    },
                    { data: newCompleted, label: { formatter: newRates } }
                ]
            });
        });
        
        // 导出图表按钮事件
        document.getElementById('exportChart').addEventListener('click', function() {
            const url = myChart.getDataURL({
                type: 'png',
                pixelRatio: 2,
                backgroundColor: '#fff'
            });
            
            const a = document.createElement('a');
            a.href = url;
            a.download = '项目完成情况.png';
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
        });
        
        // 响应窗口大小变化
        window.addEventListener('resize', function() {
            myChart.resize();
        });
    </script>
</body>
</html>